package miiteen.customsportal.Api;


import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.basic.beans.BasicPageBrowsing;
import miiteen.customsportal.basic.service.BasicPageBrowsingService;
import miiteen.demo.utils.IPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 网站访问记录
 */
@Api(tags = "网站访问记录")
@RestController
@RequestMapping("/Api/BasicPageBrowsing")
@CrossOrigin
public class BasicPageBrowsingApiController {

    @Autowired
    private BasicPageBrowsingService basicPageBrowsingService;


    //新增访问记录
    @ApiOperation("新增访问记录")
    @RequestMapping(value = "/saveRecord", method = RequestMethod.POST)
    public ResultMessage<Object> saveRecord(BasicPageBrowsing basicPageBrowsing, HttpServletRequest request){
        basicPageBrowsing.setBrowsingDate(new Date());
        basicPageBrowsing.setIpAddress(IPUtils.findIp(request));
        long result = basicPageBrowsingService.save(basicPageBrowsing);
        if (result> 0) {
            return ResultMessageTool.Success("成功", result);
        } else {
            return ResultMessageTool.Success("失败", result);
        }
    }

    //获取当天
    @ApiOperation("获取当天")
    @RequestMapping(value = "/getTodayCount", method = RequestMethod.GET)
    public ResultMessage<Object> getTodayCount(){
        BasicPageBrowsing basicPageBrowsing = basicPageBrowsingService.getTodayCount();
        int result=basicPageBrowsing.getTodayCount();
        if (result>=0) {
            return ResultMessageTool.Success("成功", result);
        } else {
            return ResultMessageTool.Success("失败", result);
        }
    }

    //获取总访问量
    @ApiOperation("获取总访问量")
    @RequestMapping(value = "/getTotalCount", method = RequestMethod.GET)
    public ResultMessage<Object> getTotalCount(){
        BasicPageBrowsing basicPageBrowsing = basicPageBrowsingService.getTotalCount();
        int result=basicPageBrowsing.getTotalCount();
        if (result>=0) {
            return ResultMessageTool.Success("成功", result);
        } else {
            return ResultMessageTool.Success("失败", result);
        }
    }
}
